package com.k8sops.backend.controller;

import com.k8sops.backend.common.Result;
import com.k8sops.backend.entity.Namespace;
import com.k8sops.backend.service.NamespaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/namespace")
public class NamespaceController {

    @Autowired
    private NamespaceService namespaceService;

    /**
     * 获取命名空间列表
     */
    @GetMapping("/list/{clusterId}")
    public Result<List<Namespace>> list(@PathVariable Long clusterId) {
        List<Namespace> namespaces = namespaceService.listNamespacesByClusterId(clusterId);
        return Result.success(namespaces);
    }

    /**
     * 同步命名空间
     */
    @PostMapping("/sync/{clusterId}")
    public Result<Boolean> sync(@PathVariable Long clusterId) {
        boolean success = namespaceService.syncNamespaces(clusterId);
        return Result.success(success);
    }

    /**
     * 创建命名空间
     */
    @PostMapping("/create")
    public Result<Boolean> create(@RequestBody Namespace namespace) {
        boolean success = namespaceService.createNamespace(namespace);
        return Result.success(success);
    }

    /**
     * 删除命名空间
     */
    @DeleteMapping("/delete/{clusterId}/{name}")
    public Result<Boolean> delete(@PathVariable Long clusterId, @PathVariable String name) {
        boolean success = namespaceService.deleteNamespace(clusterId, name);
        return Result.success(success);
    }
}
